package com.darkplanet.management.dao;

import com.darkplanet.management.domain.Project;
import com.darkplanet.management.util.JDBCUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class IProjectDaoImpl implements IProjectDao {
    QueryRunner queryRunner = new QueryRunner(JDBCUtil.getDs());

    @Override
    public void add(String project_name, String client_company, String customer_principal, String project_manager, Integer developers_num, String start_time, String project_time, Double estimated_cost, String level, String finish_time, String remark) {
        String sql = "insert into project (project_name,client_company,customer_principal,project_manager,developers_num,start_time,project_time,estimated_cost,level,finish_time,remark) values(?,?,?,?,?,?,?,?,?,?,?)";
        try {
            queryRunner.update(sql,project_name,client_company,customer_principal,project_manager,developers_num,start_time,project_time,estimated_cost,level,finish_time,remark);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void delete(Integer project_id) {
        String sql = "delete from project where project_id = ?";
        try {
            queryRunner.update(sql,project_id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


    @Override
    public void update(Integer project_id, String project_name, String client_company, String customer_principal, String project_manager, Integer developers_num, String start_time,String project_time,Double estimated_cost,String level,String finish_time,String remark) {
        String sql = "update project set project_name=?,client_company=?,customer_principal=?,project_manager=?,developers_num=?,start_time=?,project_time=?,estimated_cost=?,level=?,finish_time=?,remark=? where project_id = ?";
        try {
            queryRunner.update(sql,project_name,client_company,customer_principal,project_manager,developers_num,start_time,project_time,estimated_cost,level,finish_time,remark,project_id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public Project getProjectById(Integer project_id) {
        String sql = "select * from project where project_id = ?";
        Project project = null;
        try {
            project = queryRunner.query(sql, new BeanHandler<Project>(Project.class), project_id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return project;
    }

    @Override
    public List<Project> getAllProject() {
        String sql = "select * from project";
        List<Project> query = null;
        try {
            query = queryRunner.query(sql, new BeanListHandler<Project>(Project.class));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return query;
    }


}